package dbmanager.connectionManager.menu;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;

import dbmanager.connectionManager.TreeModel;
import dbmanager.struct.ConnectionFolder;
import dbmanager.struct.DBConnection;

public class AddConnectionItem extends JMenuItem implements ActionListener {
	private JTree tree;

	public AddConnectionItem(JTree tree) {
		super("Database Conection");
		addActionListener(this);
		this.tree = tree;
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		DefaultMutableTreeNode parent;
		if (!tree.isSelectionEmpty()) {
			TreePath path = tree.getSelectionPath();
			parent = (DefaultMutableTreeNode) path.getLastPathComponent();
		} else {
			parent = (DefaultMutableTreeNode) tree.getModel().getRoot();
		}
		String newConName = JOptionPane.showInputDialog(this, "Name of the new connection:", "Create new connection", JOptionPane.PLAIN_MESSAGE);
		DBConnection newCon = new DBConnection();
		newCon.setConName(newConName);
		((TreeModel) tree.getModel()).addConncetion(newCon,parent);
	}
}
